package org.dizena.shop.web;

import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.ttl.TtlRunnable;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dizena.common.base.enums.EnumHeaderKey;
import org.dizena.common.base.enums.EnumLogLevel;
import org.dizena.common.logger.bean.LogInfo;
import org.dizena.common.logger.bean.LogLocEvent;
import org.dizena.common.logger.bean.LogRpcEvent;
import org.dizena.common.utils.ReqUtil;
import org.dizena.shop.rpc.UserFeign;
import org.dizena.shop.rpc.dto.UserCmd;
import org.dizena.shop.rpc.dto.UserVO;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@Slf4j
@RestController
@RequestMapping("/product")
@Tag(name = "Product接口")
public class ProductController {

    @Resource
    private UserFeign userFeign;
    @Resource
    private ApplicationEventPublisher publisher;

    @Operation(summary = "日志")
    @GetMapping({"/log"})
    public String log() {
        String trace = ReqUtil.getHeader(EnumHeaderKey.TRACE_ID.getKey());
        log.info("log trace={}", trace);
        LogInfo logInfo = new LogInfo();
        logInfo.setTraceId(trace);
        LogRpcEvent event = new LogRpcEvent(this, logInfo, EnumLogLevel.INFO);
        publisher.publishEvent(event);

        LogLocEvent locEvent = new LogLocEvent(this, logInfo, EnumLogLevel.INFO);
        publisher.publishEvent(locEvent);

        return trace;
    }

    @Operation(summary = "同步")
    @GetMapping({"/seq"})
    public String seq() {
        String trace = ReqUtil.getHeader(EnumHeaderKey.TRACE_ID.getKey());
        log.info("seq trace={}", trace);
        log.info("get {}", userFeign.get("100").getBody());
        UserCmd cmd = new UserCmd();
        cmd.setAge(25);
        cmd.setName("seq");
        cmd.setId("1001");
        log.info("post {}", userFeign.post(cmd).getBody());
        LogInfo logInfo = new LogInfo();
        logInfo.setTraceId(trace);
        LogRpcEvent event = new LogRpcEvent(this, logInfo, EnumLogLevel.INFO);
        publisher.publishEvent(event);
        return trace;
    }

    @Operation(summary = "异步")
    @GetMapping({"/sync"})
    public String sync() {
        String trace = ReqUtil.getHeader(EnumHeaderKey.TRACE_ID.getKey());
        log.info("sync trace={}", trace);
        ThreadUtil.execute(TtlRunnable.get(() -> {
            log.info("get {}", userFeign.get("100").getBody());
            UserCmd cmd = new UserCmd();
            cmd.setAge(25);
            cmd.setName("seq");
            cmd.setId("1001");
            log.info("post {}", userFeign.post(cmd).getBody());
        }));
        return trace;
    }

    @Resource
    private RestTemplate restTemplate;

    @Operation(summary = "Rest")
    @GetMapping({"/rest"})
    public UserVO rest() {
        return restTemplate.getForObject("http://127.0.0.1:9001/center/user/get/1", UserVO.class);
    }
}
